package com.medica.ledproject.common;

import io.swagger.v3.oas.annotations.media.Schema;

public class ResponseEntity<T> {

    @Schema(description  = "是否成功", required = true)
    private boolean success=true;
    @Schema(description  = "错误编号", required = true)
    private int errorCode;
    @Schema(description  = "消息内容")
    private String message;
    @Schema(description  = "数据体")
    private T data;

    private ResponseEntity()
    {}

    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public int getErrorCode() {
        return errorCode;
    }

    public void setErrorCode(int errorCode) {
        this.errorCode = errorCode;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public static ResponseEntity ok(){
        return ok(null);
    }

    public static <T> ResponseEntity<T> ok(T t){
        ResponseEntity<T> item = new ResponseEntity<>();
        item.setSuccess(true);
        item.setErrorCode(0);
        item.setMessage("操作成功");
        item.setData(t);
        return item;
    }

    public static <T> ResponseEntity<T> failed(String message){
        return failed(0, message);
    }

    public static <T> ResponseEntity<T> failed(int code, String message){
        ResponseEntity<T> item = new ResponseEntity<>();
        item.setSuccess(false);
        item.setErrorCode(code);
        item.setMessage(message);
        item.setData(null);
        return item;
    }
}
